#include "stdlib.h"
#include "string.h"
#include <hidef.h> /* for EnableInterrupts macro */
#include "Headers/derivative.h" /* include peripheral declarations */
#include "Headers/define.h"
#include "Headers/GPIO.h"
#include "Headers/SPI.h"
#include "Headers/ICG.h"
#include "Headers/micelaneous.h"
#include "Headers/RF.h"
#include "Headers/time.h"
#include "Headers/ADC.h"
#include "Headers/regadera.h"
#include "Headers/232.h"
#include "Headers/receiver232.h"
char receiver232(void){
   char *command_rx, rta_fc = 0, *rs232, envio = 0;
   char *aux;
   command_rx = malloc(125);
   rs232 = malloc(10);
   *command_rx = 0;
   if(!RX_pkt_rf(command_rx)){
    aux = strtok(command_rx, "|");
    aux = strtok(NULL, "|");
    if(strcmp(aux, "tinaco") == 0){
      adc_tinaco = *command_rx;
      envio = 0;
    }else if(strcmp(aux, "regadera") == 0){
      adc_regadera = *command_rx;
      TX_pkt_rf("rs232");
      TX_pkt_rf("rs232");
      TX_pkt_rf("rs232");
      TX_pkt_rf("rs232");
      TX_pkt_rf("rs232");
      //start byte rs232
      rs232[0] = 0x02;
      //most significant 4 bits from adc trinaco
      rs232[1] = adc_tinaco >> 4;
      if(rs232[1] <= 9) rs232[1] += 48;
      else rs232[1] += 55;
      // lest significant 4 bits from adc tinaco
      rs232[2] = adc_tinaco & 0x0f;
      if(rs232[2] <= 9) rs232[2] += 48;
      else rs232[2] += 55;
      // separate charcter
      rs232[3] = '|';
      // most significant 4 bits from adc in here
      rs232[4] = adc_regadera >> 4;
      if(rs232[4] <= 9) rs232[4] += 48;
      else rs232[4] += 55;
      // lest significant 4 bits from adc in here
      rs232[5] = adc_regadera & 0x0f;
      if(rs232[5] <= 9) rs232[5] += 48;
      else rs232[5] += 55;
      // stop byte
      rs232[6] = 0x03;
      // this is for cut the string in the last value given
      rs232[7] = 0x00;
      
      //send_232("\r");
      send_232(rs232);
      delay_ms(40);
      if(ld4)ld4 = 0;
      else ld4 = 1;
    }
   }
   free(rs232);
   free(command_rx);
   return rta_fc;
}